﻿using System.Windows;
using System.Windows.Controls;
using Schuifpuzzel.Model;
using Schuifpuzzel.MyKinect;


namespace Schuifpuzzel
{
    /// <summary>
    /// Interaction logic for HighScoresScreen.xaml
    /// </summary>
    /// 
    public delegate void RequestStartPuzzleHandler();

    public partial class HighScoresScreen
    {
        private HighScoreList _highScoreList;
        private KinectColorViewer _kinectColorViewer;
        
        private TextBlock[] _nameFields;
        private TextBlock[] _timeFields;
        private TextBlock[] _movesFields;
        public event RequestStartPuzzleHandler RequestStartPuzzle;
        private int currentPictureNumber = -1;

        /// <summary>
        /// The highscore list to display
        /// </summary>
        public HighScoreList HighScoreList
        {
            set
            {
                _highScoreList = value;
                UpdateScores();
            }

        }

        public HighScoresScreen()
        {
            InitializeComponent();

            //Add a colorviewer to the screen
            _kinectColorViewer = new KinectColorViewer();
            colorViewer.Content = _kinectColorViewer;

            //Put all fields into an array to make them easier to work with
            _nameFields = new TextBlock[5];
            _nameFields[0] = txtName1;
            _nameFields[1] = txtName2;
            _nameFields[2] = txtName3;
            _nameFields[3] = txtName4;
            _nameFields[4] = txtName5;
            _timeFields = new TextBlock[5];
            _timeFields[0] = txtTime1;
            _timeFields[1] = txtTime2;
            _timeFields[2] = txtTime3;
            _timeFields[3] = txtTime4;
            _timeFields[4] = txtTime5;
            _movesFields = new TextBlock[5];
            _movesFields[0] = txtMoves1;
            _movesFields[1] = txtMoves2;
            _movesFields[2] = txtMoves3;
            _movesFields[3] = txtMoves4;
            _movesFields[4] = txtMoves5;           
            
        }

        /// <summary>
        /// Method that gets called when stretching an arm is detected
        /// </summary>
        /// <param name="hand"></param>
        void ArmStretchedDetected(Hand hand)
        {
            KinectForPuzzle kinect = KinectForPuzzle.GetInstance();
            kinect.TrackedHand = hand;
            if (RequestStartPuzzle != null)
            {
                KinectForPuzzle.GetInstance().SkeletonLocked = true;
                kinect.DistanceFromKinectDetected -= DistanceFromKinectDetected;
                kinect.ArmStretchedDetected -= ArmStretchedDetected;
                RequestStartPuzzle();
               
            }
            
        }

        /// <summary>
        /// Method that gets called when distance from the kinect has been calculated.
        /// Informs the user when he is too close or too far.
        /// </summary>
        /// <param name="distance">The distance between the user and the Kinect.</param>
        void DistanceFromKinectDetected(int distance)
        {
            if (distance != -1)
            {
                if (distance >= 6000)
                {
                    txtStatus.Text = "You are too far away, please move closer.";
                }
                else if (distance <= 1500)
                {
                    txtStatus.Text = "You are too close to the sensor, please move further away.";
                }
                else
                {
                    txtStatus.Text = "Stretch the arm you want to control the puzzle with!";
                }
            }
            else
            {
                txtStatus.Text = "Move in front of the sensor to start!";
            }
        }

        /// <summary>
        /// Displays the scores
        /// </summary>
        private void UpdateScores()
        {
            
            for (int i = 0; i < 5 && i< _highScoreList.HighScores.Count ; i++){
                _nameFields[i].Text = _highScoreList.HighScores[i].Name; 
                _timeFields[i].Text = _highScoreList.HighScores[i].Time + ""; 
                _movesFields[i].Text = _highScoreList.HighScores[i].Moves + "";
            }
        }
     
        /// <summary>
        /// Method to be called when showing the screen.
        /// Binds events to control highscreen with Kinect
        /// </summary>
        public void Start()
        {
            pictureViewer.Content = null;
            KinectForPuzzle.GetInstance().SkeletonLocked = false;
            KinectForPuzzle.GetInstance().DistanceFromKinectDetected += DistanceFromKinectDetected;
            KinectForPuzzle.GetInstance().ArmStretchedDetected += ArmStretchedDetected;
        }

        //Display the player's image when his name is clicked
        private void txtName1_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShowImage(0);
        }
        private void txtName2_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShowImage(1);
        }
        private void txtName3_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShowImage(2);
        }
        private void txtName4_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShowImage(3);
        }
        private void txtName5_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShowImage(4);
        }

        /// <summary>
        /// Display image
        /// </summary>
        /// <param name="highScoreNumber">Number of the image to be displayed</param>
        private void ShowImage(int highScoreNumber)
        {
            if (currentPictureNumber != highScoreNumber)
            {
                PictureScreen pic = new PictureScreen(_highScoreList.HighScores[highScoreNumber]);
                pictureViewer.Visibility = Visibility.Visible;
                pictureViewer.Content = pic;
                currentPictureNumber = highScoreNumber;
                if (pic.playerImage.Source == null)
                {
                    pictureViewer.Height = 240;
                    pictureViewer.Width = 320;
                }
                else
                {
                    pictureViewer.Height = 640;
                    pictureViewer.Width = 720;
                }
            }
            else
            {
                pictureViewer.Content = null;
                currentPictureNumber = -1;
                pictureViewer.Visibility = Visibility.Hidden;
            }
        }

        private void pictureViewer_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            pictureViewer.Content = null;
            currentPictureNumber = -1;
            pictureViewer.Visibility = Visibility.Hidden;
        }

             
    }
}
